# pip install python-dotenv
from dotenv import load_dotenv; load_dotenv()

from langchain.agents import Tool, create_self_ask_with_search_agent
from langchain.agents import AgentExecutor  
from langchain_community.utilities import BingSearchAPIWrapper
from langchain_openai import OpenAI
from langchain import hub
from langchain_core.runnables import RunnableConfig
import os, json

# search = BingSearchAPIWrapper()
search_kwargs = json.loads(os.environ.get("BING_SEARCH_KWARGS", "{}"))
search = BingSearchAPIWrapper(k=4, search_kwargs = search_kwargs)
tools = [  
    Tool(  
        name="Intermediate Answer",  
        func=search.run,  
        description="在需要使用搜索进行提问时很有用",  
    )  
]  

llm = OpenAI(model="gpt-3.5-turbo-instruct", temperature=0, streaming=False)
prompt = hub.pull("hwchase17/self-ask-with-search")
print(prompt.template)

agent = create_self_ask_with_search_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)

config = RunnableConfig(stream_runnable=False, max_concurrency=1)
response = agent_executor.invoke(
	{
		"input": "使用玫瑰作为国花的国家的首都是哪里?"  
	},
    config=config
)
print(response)
